<?php
include 'report_lib.php';
$a_js_files = array (
    array ( 'path' => 'scripts/nh-report.js' ),
);

$a_js_class[] = 'User_Report.init();';
    
$a_User_ChuyenCan = $a_User_HocLuc = $a_User_Info = $a_Tmp_ChuyenCan = $a_Tmp_Dot_KiemTra = $a_User_Class = array ( );

// <editor-fold defaultstate="collapsed" desc="Get Score">
$v_startdate = date( $s_category_course       . '-m-01', strtotime($a_KhoaHoc_Info['start']));
$v_enddate   = date( $s_category_course + 1   . '-m-d' , strtotime($a_KhoaHoc_Info['end'  ]));

// <editor-fold defaultstate="collapsed" desc="Get Class">
$db_query = "
    select  userid, team, level, crew, chuyencan, hocluc
    from    user_class
    where   course = '$s_category_course'
            and chuyencan <> '' and hocluc <> ''
            and (chuyencan < 5 or hocluc < 5) ";

$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {
    
    $a_User_Class[$d_row->team][$d_row->level][$d_row->crew][$d_row->userid] =  $a_User_Info[$d_row->userid] = $d_row;
}
// </editor-fold>

$db_query = "
    select   userid, scoretype, date, score, data, type
    from     user_score
    where    date >= '$v_startdate' and date <= '$v_enddate'
             and userid in ('" . implode("', '", array_keys($a_User_Info)) . "')";

$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {
    
    if ($d_row->type == 'CHUYEN_CAN') {
        @$a_User_ChuyenCan[$d_row->userid][$d_row->score]['counter'] += 1;
        @$a_User_ChuyenCan[$d_row->userid][$d_row->score]['minus'  ] += $d_row->data;
        $a_Tmp_ChuyenCan[$d_row->score] = $d_row->score;
    } else if ($d_row->type == 'HOC_LUC') {
        $a_User_HocLuc[$d_row->userid][$d_row->scoretype][$d_row->date] = $d_row->score;
        $a_Tmp_Dot_KiemTra[$d_row->scoretype] = $d_row->scoretype;
    }
}

if (!empty($a_User_HocLuc)) {
    foreach ($a_User_HocLuc as $v_userid => $a_tmp_scoretype) {
        foreach ($a_tmp_scoretype as $v_type => $a_value) {
            $a_User_HocLuc[$v_userid][$v_type] = array_sum($a_value)/count($a_value);
        }
    }
}

$a_Tmp_DiLe_DiHoc = array_merge($a_DiLe, $a_DiHoc);
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Get User Info">
$db_query = "
    select  userid, saint, username, usertype, userstatus, gender
    from    user
    where   userid in ('" . implode("', '", array_keys($a_User_Info)) . "') and $s_get_THIEU_NHI_condition
    order by firstname";

$db_result = db_query($db_conn, $db_query);

while ($d_row = db_fetch($db_result)) {
    $a_User_Info[$d_row->userid]->saint    = $d_row->saint;
    $a_User_Info[$d_row->userid]->username = $d_row->username;
    $a_User_Info[$d_row->userid]->usertype = $d_row->usertype;
    $a_User_Info[$d_row->userid]->gender   = $d_row->gender;
}
// </editor-fold>

include 'lib_head.php';
?>
<!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div id="portlet-config" class="modal hide">
    <div class="modal-header">
        <button data-dismiss="modal" class="close" type="button"></button>
        <h3>Bộ Lọc</h3>
    </div>
    <div class="modal-body">
        Điền thông tin cần thiết để lọc được dữ liệu thích hợp.
    </div>
</div>
<!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
<div class="row-fluid">
    <!-- BEGIN BORDERED TABLE PORTLET-->
    <div class="portlet box green">
        <div class="portlet-title">
            <div class="caption"><i class="icon-filter"></i>Lọc Dữ Liệu</div>
            <div class="tools">
                <a href="javascript:;" class="collapse default_expand"></a>
                <a href="#portlet-config" data-toggle="modal" class="config"></a>
            </div>
        </div>
        <div class="portlet-body">
            <form action="" style="margin: 0">
                <div class="pull-left margin-right-20">
                    <div class="control-group">
                        <label class="control-label" >Khóa Học</label>
                        <div class="controls">
                            <select name="category_course" class="m-wrap span12">
                            <?php
                            $v_tmp_start_course = $a_KhoaHoc_Info['start_year'];

                            for ($v_index = $v_tmp_start_course; $v_index > $v_tmp_start_course - 10 ; $v_index--) { ?>
                                <option <?= $s_category_course == $v_index ? 'selected="selected"' : '' ?> value="<?= $v_index ?>"><?= $v_index ?> - <?= $v_index + 1?></option>
                            <?php } ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="pull-left">
                    <div class="control-group">
                        <label class="control-label">&ensp;</label>
                        <div class="controls">
                            <div class="input-prepend">
                                <button class="btn blue">BẮT ĐẦU LỌC <i class="m-icon-swapright m-icon-white"></i></button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </form>
        </div>
    </div>
    <!-- END BORDERED TABLE PORTLET-->
    <div class="portlet-body">
        <?php if (!empty($a_User_Class)) { ?>
        <table class="table table-bordered table-hover">
        <?php
            $i_Colspan_ChuyenCan = count($a_Tmp_ChuyenCan);
            $i_Colspan_HocLuc    = count($a_Tmp_Dot_KiemTra);

            foreach ($a_User_Class as $v_team => $a_CapID) {
                foreach ($a_CapID as $v_level => $a_DoiID) {
                    foreach ($a_DoiID as $v_crew => $a_User_Infos) {

                    $v_tmp_classname = '';

                    if ( isset($a_Nganh [$v_team ]) ) { $v_tmp_classname = $v_tmp_classname .           $a_Nganh    [$v_team ]; }
                    if ( isset($a_Cap   [$v_level]) ) { $v_tmp_classname = $v_tmp_classname . ' - '   . $a_Cap      [$v_level]; }
                    if ( isset($a_Doi   [$v_crew ]) ) { $v_tmp_classname = $v_tmp_classname . ' - '   . $a_Doi      [$v_crew ]; }
        ?>
            <thead>
                <tr>
                    <th style="vertical-align: middle" rowspan="2"><?=$v_tmp_classname?></th>
                    <th style="text-align: center" colspan="<?=$i_Colspan_ChuyenCan?>" class="hidden-phone">Chuyên Cần</th>
                    <th style="text-align: center; vertical-align: middle" rowspan="2">Tổng Ch/Cần</th>
                    <th style="text-align: center" colspan="<?=$i_Colspan_HocLuc?>" class="hidden-phone">Học Tập</th>
                    <th style="text-align: center; vertical-align: middle" rowspan="2">TB H/Tập  </th>
                </tr>
                <tr>
                    <?php foreach ($a_Tmp_ChuyenCan as $v_value) { ?>
                    <th style="text-align: center" class="hidden-phone"><?=@$a_Tmp_DiLe_DiHoc[$v_value]?></th>
                    <?php } ?>
                    <?php foreach ($a_Tmp_Dot_KiemTra as $v_value) { ?>
                    <th style="text-align: center" class="hidden-phone"><?=@$a_Dot_KiemTra[$v_value]?></th>
                    <?php } ?>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($a_User_Infos as $v_userid => $d_tmp_row) { ?>
                <tr>
                    <td><span class="hidden-phone"><?=$d_tmp_row->saint?></span> <?=$d_tmp_row->username?></td>
                    <?php foreach ($a_Tmp_ChuyenCan as $v_value) { ?>
                    <td class="hidden-phone">
                        <div class="text-center" style="white-space: nowrap">
                        <?php if (isset($a_User_ChuyenCan[$v_userid][$v_value])) {?>
                        <?= $a_User_ChuyenCan[$v_userid][$v_value]['counter'] ?>
                        |
                        <?= $a_User_ChuyenCan[$v_userid][$v_value]['minus'] ?>
                        <?php } ?>
                        </div>
                    </td>
                    <?php } ?>
                    <td style="text-align: center"><?=$d_tmp_row->chuyencan?></td>
                    <?php foreach ($a_Tmp_Dot_KiemTra as $v_value) { ?>
                    <td style="text-align: center" class="hidden-phone">
                    <?= @$a_User_HocLuc[$v_userid][$v_value] ?>
                    </td>
                    <?php } ?>
                    <td style="text-align: center"><?=$d_tmp_row->hocluc?></td>
                </tr>
                <?php } ?>
            </tbody>
            <tr>
                <th colspan="<?= $i_Colspan_ChuyenCan + $i_Colspan_HocLuc + 3?>"></th>
            </tr>
        <?php
                }
            }
        }
        ?>
        </table>
    <?php } ?>
    </div>
</div>
<?php include 'lib_tail.php'; ?>